package com.tbit.uqbike.object.business;

import com.tbit.uqbike.constant.BikeStateConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.webmanager.util.StringUtils;

import java.util.*;

/**
 * @author MyWin E-mail:335918956@qq.com
 * @version 1.0
 * @createTime 2019-12-26 20:15
 */
public class MachineImportInfo {
    public static final String COL_MACHINENO_CODE = "excel.col.machineNO";
    public static final String COL_USERCODE_CODE = "excel.col.userCode";
    public static final String COL_FRAME_CODE = "excel.col.frame";
    public static final String COL_LICENSE_CODE = "excel.col.license";
    public static final String COL_MOTOR_CODE = "excel.col.motor";
    public static final String COL_REMARK_CODE = "excel.col.remark";
    public static final String COL_STATUS_CODE = "excel.col.status";

    // 是否投放
    public static final String COL_STATUS_YES = "YES";
    public static final String COL_STATUS_NO = "NO";

    public static final int ADD_COL_MACHINENO_INDEX = 0;
    public static final int ADD_COL_USERCODE_INDEX = 1;
    public static final int ADD_COL_FRAME_INDEX = 2;
    public static final int ADD_COL_LICENSE_INDEX = 3;
    public static final int ADD_COL_MOTOR_INDEX = 4;
    public static final int ADD_COL_REMARK_INDEX = 5;

    public static final int MOD_COL_USERCODE_INDEX = 0;
    public static final int MOD_COL_STATUS_INDEX = 1;


    public static final int EDIT_COL_USERCODE_INDEX = 0;
    public static final int EDIT_COL_FRAME_INDEX = 1;
    public static final int EDIT_COL_LICENSE_INDEX = 2;
    public static final int EDIT_COL_MOTOR_INDEX = 3;
    public static final int EDIT_COL_REMARK_INDEX = 4;

    public String machineNO;
    public String userCode;
    public Integer bikeState;
    /**
     * 车架号
     */
    public String frame;
    /**
     * 车牌号
     */
    public String license;
    /**
     * 电机号
     */
    public String motor;
    /**
     * 车辆描述
     */
    public String remark;

    public MachineImportInfo() {
    }

    public MachineImportInfo(String machineNO, String userCode) {
        this.machineNO = machineNO;
        this.userCode = userCode;
    }

    public MachineImportInfo(String machineNO, String userCode, String frame, String license,
                             String motor, String remark) {
        this.machineNO = machineNO;
        this.userCode = userCode;
        this.frame = frame;
        this.license = license;
        this.motor = motor;
        this.remark = remark;
    }

    public MachineImportInfo(String userCode, Integer bikeState) {
        this.userCode = userCode;
        this.bikeState = bikeState;
    }

    public static MachineImportInfo initModInfo(String code, String status) {
        Integer bikeState = null;
        if (Objects.equals(status, MachineImportInfo.COL_STATUS_YES)) {
            bikeState = BikeStateConstant.BIKE_STATE_READY_FREE;
        } else if (Objects.equals(status, MachineImportInfo.COL_STATUS_NO)) {
            bikeState = BikeStateConstant.BIKE_STATE_NOT_READY;
        }
        if (null == bikeState) {
            throw new BaseException(SystemCodeEnum.EXCLE_MODEL_ERROR);
        }
        return new MachineImportInfo(code, bikeState);
    }

    public static List<MachineImportInfo> turnAddMachineList(List<List<String>> data) {
        List<MachineImportInfo> list = new LinkedList<>();
        Set<String> mnoSet = new HashSet<>();
        Set<String> codeSet = new HashSet<>();
        for (List<String> items : data) {
            int i = 0;
            String mno = null;
            String code = null;
            /**车架号*/
            String frame = null;
            /**车牌号*/
            String license = null;
            /**电机号*/
            String motor = null;
            /**车辆描述*/
            String remark = null;
            for (String item : items) {
                if (item == null) {
                    break;
                }
                String tItem = item.trim();
                /*if (tItem.isEmpty()) {
                    break;
                }*/
                if (i == ADD_COL_MACHINENO_INDEX) {
                    mno = tItem;
                } else if (i == ADD_COL_USERCODE_INDEX) {
                    code = tItem;
                } else if (i == ADD_COL_FRAME_INDEX) {
                    frame = tItem;
                } else if (i == ADD_COL_LICENSE_INDEX) {
                    license = tItem;
                } else if (i == ADD_COL_MOTOR_INDEX) {
                    motor = tItem;
                } else if (i == ADD_COL_REMARK_INDEX) {
                    remark = tItem;
                }
                i++;
            }
            if (null != mno && null != code) {
                /**车辆编码有YES、ON认为Excel格式有问题*/
                if ("YES".equalsIgnoreCase(code) || "NO".equalsIgnoreCase(code)) {
                    throw new BaseException("excel_model_error");
                }

                if (!mnoSet.contains(mno) && !codeSet.contains(code)) {
                    mnoSet.add(mno);
                    codeSet.add(code);
                    list.add(new MachineImportInfo(mno, code, frame, license, motor, remark));
                }
            }
            if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mno)) {
                throw new BaseException("excel.col.null");
            }
        }
        return list;
    }

    public static List<MachineImportInfo> turnModMachineList(List<List<String>> data) {
        List<MachineImportInfo> list = new LinkedList<>();
        Set<String> codeSet = new HashSet<>();
        for (List<String> items : data) {
            int i = 0;
            String status = null;
            String code = null;
            for (String item : items) {
                if (item == null) {
                    break;
                }
                String tItem = item.trim();
                if (tItem.isEmpty()) {
                    break;
                }
                if (i == MOD_COL_USERCODE_INDEX) {
                    code = tItem;
                } else if (i == MOD_COL_STATUS_INDEX) {
                    status = tItem;
                }
                i++;
            }
            if (null != status && null != code) {
                if (!codeSet.contains(code)) {
                    codeSet.add(code);
                    list.add(initModInfo(code, status));
                }
            }
        }
        return list;
    }


    /**
     * 修改
     *
     * @param data
     * @return
     */
    public static List<MachineImportInfo> turnEditMachineList(List<List<String>> data) {
        List<MachineImportInfo> list = new LinkedList<>();
        Set<String> mnoSet = new HashSet<>();
        Set<String> codeSet = new HashSet<>();
        for (List<String> items : data) {
            int i = 0;
            String code = null;
            /**车架号*/
            String frame = null;
            /**车牌号*/
            String license = null;
            /**电机号*/
            String motor = null;
            /**车辆描述*/
            String remark = null;
            for (String item : items) {
                if (item == null) {
                    break;
                }
                String tItem = item.trim();
                if (i == EDIT_COL_USERCODE_INDEX) {
                    code = tItem;
                } else if (i == EDIT_COL_FRAME_INDEX) {
                    frame = tItem;
                } else if (i == EDIT_COL_LICENSE_INDEX) {
                    license = tItem;
                } else if (i == EDIT_COL_MOTOR_INDEX) {
                    motor = tItem;
                } else if (i == EDIT_COL_REMARK_INDEX) {
                    remark = tItem;
                }
                i++;
            }
            if (null != code) {
                /**车辆编码有YES、ON认为Excel格式有问题*/
                if ("YES".equalsIgnoreCase(code) || "NO".equalsIgnoreCase(code)) {
                    throw new BaseException("excel_model_error");
                }

                if (!codeSet.contains(code)) {
                    codeSet.add(code);
                    list.add(new MachineImportInfo(null, code, frame, license, motor, remark));
                }
            }
        }
        return list;
    }
}
